<script>
	import config from './config'
	import store from '@/store'
	import {
		getToken
	} from '@/utils/auth'
	// #ifdef APP-PLUS
	import APPUpdate from '@/uni_modules/zhouWei-APPUpdate/js_sdk/appUpdate'
	// #endif
	import {
		checkAgreement,
		authDetails
	} from '@/api/mine/auth.js'
	import {
		appConfig
	} from '@/api/document'
	import {
		debuggerModule,
		installDebugger
	} from 'uni_modules/imengyu-IMDebuggerWindow/common/debuggerExtern.js'
	export default {
		onLaunch: function() {
			// #ifdef APP-PLUS
			APPUpdate();
			// installDebugger({
			// 	enableRequestInterceptor: false, //默认为false，指示是否拦截网络请求，参见下一条
			// 	showGlobalFloatWindow: true //默认为true，指定是否添加一个全局的调试按钮，点击可跳转至窗口
			// });
			this.handlePush()
			// #endif
			this.initApp()
			this.getAppConfig()
		},
		methods: {
			handlePush() {
				uni.onPushMessage((res) => {
					console.log(res)
				})
				// 监听点击推送消息事件
				plus.push.addEventListener('click', function(msg) {
					// 处理点击推送消息后的逻辑
					console.log("click:" + JSON.stringify(msg));
					// 延时跳转页面
					setTimeout(() => {
						uni.navigateTo({
							url: `pages/charging/chargeCoupon?data=${JSON.parse(msg.payload)}`
						})
					}, 1000);
				}, false);

				// 监听在线消息事件
				plus.push.addEventListener('receive', function(msg) {
					// 业务代码
					console.log("receive:" + JSON.stringify(msg));
				}, false);

			},
			getAppConfig() {
				// 获取应用配置
				appConfig().then(res => {
					// 更新客服电话
					this.globalData.config.SERVICE_TEL = res?.data?.customer_service?.mobile
				}).catch(() => {

				})
			},
			// 初始化应用
			initApp() {
				// 初始化应用配置
				this.initConfig()
				// #ifdef APP
				this.checkLogin()
				// #endif

			},
			initConfig() {
				this.globalData.config = config

			},
			checkLogin() {
				if (!getToken()) {
					this.$tab.reLaunch('/pages/login/index')
				} else {
					// 如果是登录状态，判断是否签署协议，实名认证等
					authDetails().then(res => {
						let {
							data: {
								tcp = {}, // 嵌套默认值
								identity = {},
								data: data_auth = {},
								life_photos = {},
								vivid_photos = {},
								video = {},
							} = {}
						} = res || {} // 最外层也设置默认值
						let tcpStatus = tcp?.status
						let identityStatus = identity?.status ?? 2
						let dataStatus = data_auth?.status ?? 2
						let lifeStatus = life_photos?.status ?? 2
						let imageStatus = vivid_photos?.status ?? 2
						let videoStatus = video?.status ?? 2
						// 检查签署协议情况
						if (tcpStatus === 1) {
							// 已签署
							const statusList = [identityStatus, dataStatus, lifeStatus, imageStatus, videoStatus]
							let indexResule = statusList.indexOf(2)
							// 判断是否至少有一个环节为审核被驳回或未填写
							if (indexResule !== -1) {
								switch (indexResule) {
									case 0:
										// 实名认证被驳回
										this.$tab.redirectTo('./steps/index?stepsNumber=1')
										break;
									case 1:
										// 基本资料
										this.$tab.redirectTo('./steps/index?stepsNumber=2&current=1')
										break;
									case 2:
										// 形象照片
										this.$tab.redirectTo('./steps/index?stepsNumber=2&current=2')
										break;
									case 3:
										// 生活圈
										this.$tab.redirectTo('./steps/index?stepsNumber=2&current=3')
										break;
									case 4:
										// 生活视频
										this.$tab.redirectTo('./steps/index?stepsNumber=2&current=4')
										break;
									default:
										this.$tab.redirectTo('./steps/index?stepsNumber=3')
										break;
								}
							} else if (statusList.some(element => element === 0)) {
								// 只要有一个页面在审核中
								// 跳转至审核中页面
								this.$modal.closeLoading()
								this.$tab.redirectTo('./steps/index?stepsNumber=3')

							} else {
								// 进入首页
								this.$modal.closeLoading()
								this.$tab.reLaunch('/pages/index/index')
							}

						} else {
							// 未签署，跳转签署服务协议页
							this.$modal.closeLoading()
							this.$tab.redirectTo('merchant-agreement')
						}
					}).catch(() => this.$modal.closeLoading())
				}
			}
		}
	}
</script>

<style lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";
	@import '@/static/scss/index.scss';
	@import "@/common/css/ui.css";

	page {
		color: #202020 !important;
		background-color: #F5F6F7;
	}

	// ::v-deep .uni-sample-toast {
	// 	max-width: 432px !important;
	// 	padding: 20rpx !important;
	// 	box-sizing: border-box !important;
	// 	text-align: center !important;
	// 	color: #fff !important;
	// 	font-size: 28rpx !important;
	// 	background: #000000 !important;
	// 	box-shadow: 0rpx 8rpx 24rpx 0rpx rgba(0, 0, 0, 0.3) !important;
	// 	border-radius: 8rpx !important;
	// 	opacity: 0.9 !important;

	// 	uni-toast .uni-simple-toast__text {
	// 		font-size: 28rpx !important;
	// 		padding: 0 !important;
	// 	}
	// }
</style>